<html> 
<head>
<title>Movie Database</title>
</head>

<?php
$query = isset($_POST["query"]) ? $_POST["query"] : '';
?>

<body>
<h1>Movie Database</h1>

<form method="POST">
<textarea name="query" cols="60" rows="8"><?php print $query ?></textarea>
<input type="submit" value="Submit" />
</form>

<?php 
$db_connection = mysql_connect("localhost", "cs143", "");
mysql_select_db("CS143", $db_connection);

if (empty($query))
	return;
$rs = mysql_query($query, $db_connection);

echo "<table border=\"1\">\n";
$line = mysql_fetch_array($rs, MYSQL_ASSOC);
echo "\t<tr>\n";
foreach (array_keys($line) as $col_value) {
	echo "\t\t<th>$col_value</th>\n";
}
echo "\t</tr>\n";

do {
	echo "\t<tr>\n";
	foreach ($line as $col_value) {
		echo (empty($col_value)) ? "\t\t<td>N/A</td>\n" :
			"\t\t<td>$col_value</td>\n";
	}
	echo "\t</tr>\n";
} while ($line = mysql_fetch_array($rs, MYSQL_ASSOC));
echo "</table>\n";

mysql_close($db_connection);
?>

</body>
</html>
